
package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webshop.data.OrderInvoice;
import webshop.data.Orderline;
import java.util.*;



/**
 * Servlet that listst all the orderInvoices.
 * @author vighi & ohm.
 */
public class OrderInvoiceListServlet extends AbstractSuperServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();

        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        out.println("<html><head><title>"+this.getClass().getName()+"</title></head><body>");
        out.println("<h1>OrderInvoice List</h1>");

        out.println("<table>");
        out.println("<tr><th>OrderInvoice</th></tr>");

        ArrayList products = new ArrayList();

        // Gets the Invocises from the db.
        Query q = em.createNamedQuery("OrderInvoice.selectAll");
        List<OrderInvoice> orderInvoices = q.getResultList();

        for (OrderInvoice orderInvoice : orderInvoices) {
            out.println("<table border=1 cellspacing=4>");
            // Prints out orderinvoice id, basket id and person name.
            out.println("<tr><td><b>OrderInvoice ID</b>: "+orderInvoice.getOrderInvoiceId()+"</td><td> ShoppingBasket Id: "+
            orderInvoice.getShoppingBasketId()+ "</td></tr> <tr><td>Customer name: <br/>" + orderInvoice.getShoppingBasket().getPerson().getName()+
            "</td></tr>" + "<tr><td>Products: <br/>");

            // Gets all the orderlines form db and checks if they are in the current
            // shoppingbasket, prints out if they are.
            Query q2 = em.createNamedQuery("Orderline.selectAll");
            List<Orderline> orderlines = q2.getResultList();
            for (Orderline orderline : orderlines){
                if(orderInvoice.getShoppingBasketId() == orderline.getShoppingBasketId())
                    out.println(orderline.getProduct().getProductId() + "  "+ orderline.getProduct().getName() + "<br/>");
            }

            out.println("</tr><br/><br/>");
        }
        out.println("</table>");

        out.println("<hr/>");
        out.println("<p><a href=\"index.html\">Home</a></p>");
        out.println("</body>");
        out.println("</html>");

        em.close();
        out.close();
        }
    } 




